class TimeUtils {
  index = 0;
  timer = {}
  constructor() { }

  countdown(self, endTime) {

    var that = this;
    var EndTime = endTime;
    var NowTime = new Date().getTime();
    var second = EndTime - NowTime || [];

    that.timer = setInterval(function () {
      second -= 1000;
      if (second <= 0) {
        console.log('剩余时间：' + second);
        self.setData({
          clock: "已经截止",
          second: 0
        });
        if (that.timer) {
          clearInterval(that.timer);
        }
      } else {
        // 渲染倒计时时钟
        self.setData({
          clock: that.dateformat(second),
          second: second,
        });
      }
    }, 1000);
  }
  


  countdownForList(self, groups, index) {
    var that = this;
    this.index = index;
    var nowTime = new Date().getTime();
    var second = groups.endTime - nowTime || [];

    this.timer = setInterval(function () {
      second -= 1000;
      // 渲染倒计时时钟
      self.data.groupsMemberList[that.index].clock = that.dateformatToHours(second);
      self.data.groupsMemberList[that.index].second = second;
      // console.log(self.data.groupsMemberList)

      self.setData({
        groupsMemberList: self.data.groupsMemberList
      })

      if (second <= 0) {
        // console.log('剩余时间：' + second);
        clearInterval(that.timer);
        self.data.groupsMemberList[that.index].clock = "已经截止";
        self.data.groupsMemberList[that.index].second = second;
        // console.log(self.data.groupsMemberList)
        self.setData({
          groupsMemberList: self.data.groupsMemberList
        })
      }

    }, 1000);

  }

  countdownForHMS(self, endTime) {

    var that = this;
    var EndTime = endTime;
    var NowTime = new Date().getTime();
    var currentSecond = EndTime - NowTime || [];

    var timer = setInterval(function () {
      currentSecond -= 1000;

      let t = that.dateformat2(currentSecond)
      // 渲染倒计时时钟
      self.setData({
        hours: t[1] + t[0] * 24,
        minute: t[2],
        second: t[3]
      });

      if (currentSecond <= 0) {
        clearInterval(timer);
        self.setData({
          clock: "已经截止",
          currentSecond: 0,
        });
      }

    }, 1000);
  }

  countdownForDHMS(self, endTime) {

    var that = this;
    var EndTime = endTime;
    var NowTime = new Date().getTime();
    var currentSecond = EndTime - NowTime || [];
    self.data.timer = setInterval(function () {
      currentSecond -= 100;

      let t = that.dateformat2(currentSecond)
      // 渲染倒计时时钟
      self.setData({
        day: t[0],
        hours: t[1],
        minute: t[2],
        second: t[3],
        micro: t[4]
      });

      if (currentSecond <= 0) {
        self.setData({
          clock: "已经截止",
          currentSecond: 0,
        });

        if (self.clearTimeCallback) {
          if (!self.data.isCallOver) {
            self.clearTimeCallback()
            self.data.isCallOver = true
          }
        }

        if (self.data.timer > 0) {
          clearInterval(self.data.timer);
        }

      }

    }, 100);
  }

  dateformat2(micro_second) {
    // 总秒数
    var second = Math.floor(micro_second / 1000);
    // 天数
    var day = Math.floor(second / 3600 / 24);
    // 小时
    var hr = Math.floor(second / 3600 % 24);
    // 分钟
    var min = Math.floor(second / 60 % 60);
    // 秒
    var sec = Math.floor(second % 60);
    // 毫秒位，保留1位
    var micro_sec = Math.floor((micro_second % 1000) / 100);
    if ((hr + '').length == 1) {
      hr = "0" + hr;
    }

    if ((min + '').length == 1) {
      min = "0" + min;
    }

    if ((sec + '').length == 1) {
      sec = "0" + sec;
    }
    return [day, hr, min, sec, micro_sec];
  }


  dateformat(micro_second) {
    // 总秒数
    var second = Math.floor(micro_second / 1000);
    // 天数
    var day = Math.floor(second / 3600 / 24);
    // 小时
    var hr = Math.floor(second / 3600 % 24);
    // 分钟
    var min = Math.floor(second / 60 % 60);
    // 秒
    var sec = Math.floor(second % 60);
    // 毫秒位，保留2位
    var micro_sec = Math.floor((micro_second % 1000) / 10);
    if ((hr + '').length == 1) {
      hr = "0" + hr;
    }

    if ((min + '').length == 1) {
      min = "0" + min;
    }

    if ((sec + '').length == 1) {
      sec = "0" + sec;
    }

    if (day == 0) {
      return hr + "时" + min + "分" + sec;
    }
    if (day == 0 && hr == '00') {
      return min + "分" + sec;
    }
    return day + "天" + hr + "时" + min + "分" + sec;
  }

  dateformatToHours(micro_second) {
    // 总秒数
    var second = Math.floor(micro_second / 1000);
    // 天数
    var day = Math.floor(second / 3600 / 24);
    // 小时
    var hr = Math.floor(second / 3600 % 24) + day * 24;
    // 分钟
    var min = Math.floor(second / 60 % 60);
    // 秒
    var sec = Math.floor(second % 60);
    // 毫秒位，保留2位
    var micro_sec = Math.floor((micro_second % 1000) / 10);
    if ((hr + '').length == 1) {
      hr = "0" + hr;
    }

    if ((min + '').length == 1) {
      min = "0" + min;
    }

    if ((sec + '').length == 1) {
      sec = "0" + sec;
    }
    return hr + "时" + min + "分" + sec;
  }




  //数据转化  
  formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  }
  /** 
   * 时间戳转化为年 月 日 时 分 秒 
   * number: 传入时间戳 
   * format：返回格式，支持自定义，但参数必须与formateArr里保持一致 
  */
  formatTime(number, format) {

    var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
    var returnArr = [];

    var date = new Date(number);
    returnArr.push(date.getFullYear());
    returnArr.push(this.formatNumber(date.getMonth() + 1));
    returnArr.push(this.formatNumber(date.getDate()));

    returnArr.push(this.formatNumber(date.getHours()));
    returnArr.push(this.formatNumber(date.getMinutes()));
    returnArr.push(this.formatNumber(date.getSeconds()));

    for (var i in returnArr) {
      format = format.replace(formateArr[i], returnArr[i]);
    }
    return format;
  }

  getDayName(time) {
    var currentday = this.formatTime(new Date().getTime(), 'D')
    var timeDay = this.formatTime(time, 'D')
    let currentHms = this.formatTime(time, 'h:m')
    var showHint
    if (timeDay == currentday) {
      showHint = "今天 " + currentHms
    } else if (timeDay - 1 == currentday) {
      showHint = "明天 " + currentHms
    } else {
      showHint = this.formatTime(time, 'M月D号 h:m')
    }
    return showHint
  }

  /**
  * 格式化timestap
  * @param dateTimeStamp 要转换的时间缀
  * @returns {*}
  */
  formatTimestap(dateTimeStamp) {
    var result;
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var now = new Date().getTime();
    var diffValue = now - dateTimeStamp;
    if (diffValue < 0) {
      return;
    }
    var monthC = diffValue / month;
    var weekC = diffValue / (7 * day);
    var dayC = diffValue / day;
    var hourC = diffValue / hour;
    var minC = diffValue / minute;
    if (monthC >= 1) {
      if (monthC <= 12)
        result = "" + parseInt(monthC) + "月前";
      else {
        result = "" + parseInt(monthC / 12) + "年前";
      }
    }
    else if (weekC >= 1) {
      result = "" + parseInt(weekC) + "周前";
    }
    else if (dayC >= 1) {
      result = "" + parseInt(dayC) + "天前";
    }
    else if (hourC >= 1) {
      result = "" + parseInt(hourC) + "小时前";
    }
    else if (minC >= 1) {
      result = "" + parseInt(minC) + "分钟前";
    } else {
      result = "刚刚";
    }

    return result;
  };
}




export default TimeUtils